package cn.itcast.mapper;

import cn.itcast.domain.Dept;
import cn.itcast.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    //查询所有
    @Select("select * from ss_user")
    @Results({
            @Result(id=true,column = "user_id",property = "id"),
            @Result(column = "email",property = "email"),
            @Result(column = "user_name",property = "userName"),
            @Result(column = "gender",property = "gender"),
            @Result(column = "state",property = "state"),
            @Result(property = "dept",javaType = Dept.class,column = "dept_id",
                one = @One(select = "cn.itcast.mapper.DeptMapper.findByUid")),
    })
    List<User> findAll();

    //添加用户
    @Insert(" insert into ss_user (user_id, email, user_name,  password, state,\n" +
            "        gender, telephone, birthday, join_date, dept_id\n" +
            "        )\n" +
            "        values (#{id,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR},\n" +
            "        #{password,jdbcType=VARCHAR}, #{state,jdbcType=DECIMAL}, #{gender,jdbcType=CHAR},\n" +
            "        #{telephone,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR}, #{joinDate,jdbcType=VARCHAR},\n" +
            "        #{deptId,jdbcType=VARCHAR}")
    void add(User user);

    @Select("select * from ss_user where user_id = #{id}")
    @Results({
            @Result(id = true,column = "user_id" ,property = "id"),
            @Result(column = "user_id" ,property = "id"),
            @Result(column = "user_name" ,property = "userName"),
            @Result(column = "join_date" ,property = "joinDate"),
            @Result(column = "dept_id" ,property = "deptId"),
            @Result( property = "dept",column = "dept_id",
            one = @One(select = "cn.itcast.mapper.DeptMapper.findByUid"))
    })
    User findById(String id);

    @Update("update ss_user set email=#{email},user_name = #{userName},password = #{password},state=#{state}," +
            "gender=#{gender},telephone=#{telephone},birthday=#{birthday},join_date=#{joinDate} where user_id = #{id}  ")
    void update(User user);

    void deleteByIds(String[] sts);
}
